/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import path from 'path';

import { expect } from '@jest/globals';


import { runHvigorAsync} from '../../common/common';
import { addOhPackageDps, commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  }
};

/**
 * 1、创建stage工程
 * 2、entry模块中build-profile.json5配置：
 * "arkOptions": {
 *   "reExportCheckMode": "strict",
 *   "autoLazyImport": false
 * }
 * 3、hvigor-config.json5中配置：
 * "properties": {
 *     "ohos.arkCompile.emptyBundleName": true
 *   }
 * 4、构建hap
 * 5、不改动重新构建hap，看是否增量跳过
 * 6、三个配置项分别改一遍，看compile arkts增量是否生效
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const entryRootPath = project.getModule('entry').moduleRootPath();
  const hvigorConfigPath = path.resolve(projectRoot, 'hvigor');

  // entry模块中增加arkOptions配置项
  await commonJson5Operation(path.resolve(entryRootPath, 'build-profile.json5'), async (json5: any) => {
    json5.buildOption = {
      'arkOptions': {
        'reExportCheckMode': 'noCheck',
        'autoLazyImport': false
      }
    };
  });

  // hvigor-config中增加emptyBundleName
  await commonJson5Operation(path.resolve(hvigorConfigPath, 'hvigor-config.json5'), async (json5: any) => {
    json5.properties = {
      'ohos.arkCompile.emptyBundleName': true
    };
  });

  // 构建hap
  const commandsHap = '--mode module -p product=default -p module=entry@default assembleHap'.split(' ');
  const hapResult1 = await runHvigorAsync(projectRoot, commandsHap);
  expect(hapResult1.stdout).toContain('Finished :entry:default@CompileArkTS');

  // 二次构建，增量跳过arkcompile任务
  const hapResult2 = await runHvigorAsync(projectRoot, commandsHap);
  expect(hapResult2.stdout).toContain('UP-TO-DATE :entry:default@CompileArkTS');

  // 更改hvigor-config中emptyBundleName
  await commonJson5Operation(path.resolve(hvigorConfigPath, 'hvigor-config.json5'), async (json5: any) => {
    json5.properties = {
      'ohos.arkCompile.emptyBundleName': false
    };
  });
  const hapResult3 = await runHvigorAsync(projectRoot, commandsHap);
  expect(hapResult3.stdout).toContain('Finished :entry:default@CompileArkTS');

  // entry模块中更改arkOptions配置项
  await commonJson5Operation(path.resolve(entryRootPath, 'build-profile.json5'), async (json5: any) => {
    json5.buildOption = {
      'arkOptions': {
        'reExportCheckMode': 'compatible'
      }
    };
  });
  const hapResult4 = await runHvigorAsync(projectRoot, commandsHap);
  expect(hapResult4.stdout).toContain('Finished :entry:default@CompileArkTS');

  // entry模块中更改arkOptions配置项
  await commonJson5Operation(path.resolve(entryRootPath, 'build-profile.json5'), async (json5: any) => {
    json5.buildOption = {
      'arkOptions': {
        'autoLazyImport': true
      }
    };
  });
  const hapResult5 = await runHvigorAsync(projectRoot, commandsHap);
  expect(hapResult5.stdout).toContain('Finished :entry:default@CompileArkTS');



};

TestCaseBuilder.builder()
  .caseName('refactor_rollup_projectConfig')
  .description('重构与etsTransform相关的用户配置项')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.AR).build())
  .testFun(testfun)
  .load();